﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class ProvinceInsertViewModel : NotificationObject
    {
        ProvinceInsert gWindow;
        ProvinceViewModel gParentViewModel;

        public ProvinceInsertViewModel(ProvinceInsert pWindow, ProvinceViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            List<WArea> list = WArea.GetAll();
            foreach(var item in list)
            {
                AreaNames.Add(item.AreaName.Trim());
            }
        }

        private String provinceName;
        public String ProvinceName
        {
            get
            {
                return provinceName;
            }
            set
            {
                provinceName = value;
                RaisePropertyChanged("ProvinceName");
            }
        }

        private int personnelUID;
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        private List<String> areaNames = new List<String>();
        public List<String> AreaNames
        {
            get
            {
                return areaNames;
            }

            set
            {
                areaNames = value;
                RaisePropertyChanged("AreaNames");
            }
        }

        private String areaSelected;
        public String AreaSelected
        {
            get
            {
                return areaSelected;
            }

            set
            {
                areaSelected = value;
                RaisePropertyChanged("AreaSelected");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            List<WProvince> list = new List<WProvince>();
            WProvince item = new WProvince();
            item.ProvinceName = ProvinceName;
            item.PersonnelUID = PersonnelUID;
            item.AreaUID = WArea.GetAreaUID(AreaSelected);
            list.Add(item);
            HttpResponse response = WProvince.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
